package com.bdqn.gulimall.order.web;

import com.alibaba.fastjson.JSON;
import com.bdqn.gulimall.order.service.OrderService;
import com.bdqn.gulimall.order.vo.OrderConfirmVo;
import com.bdqn.gulimall.order.vo.OrderSubmitVo;
import com.bdqn.gulimall.order.vo.SubmitOrderResponseVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {

    @Resource
    OrderService orderService;


    /**
     * 取结算确认页
     * @param model
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {

       OrderConfirmVo confirmVo = orderService.confirmOrder();
        System.out.println("OrderConfirmVo confirmVo"+JSON.toJSONString(confirmVo));
       model.addAttribute("orderConfirmData",confirmVo);
        //展示订单确认的数据
        return "confirm";
    }

    /**
     * 下单功能
     * @param vo
     * @return
     */
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo vo){

        SubmitOrderResponseVo submitOrder =orderService.submitOrder(vo);
        System.out.println("订单提交的数据==>"+JSON.toJSONString(vo));
        if(submitOrder.getCode()==0){
            //下单成功就去支付页面
            return "pay";
        }else{
            //下单失败
            return "redirect:http://order.yunmall.com/toTrade";
        }
    }

}
